Imports ComponentGo.Calendars
Imports Phoenix.DataAccess
Namespace Forms.Area

    Public Class DayCalendarArea : Inherits CalendarAreaBase
        Private currPoint As New Point
#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            Calendar = DailyCalendar
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents DailyCalendar As ComponentGo.Calendars.DailyCalendar
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.DailyCalendar = New ComponentGo.Calendars.DailyCalendar
            Me.SuspendLayout()
            '
            'DailyCalendar
            '
            Me.DailyCalendar.AllDayVisible = False
            Me.DailyCalendar.DateLabelFormat = "{0:dddd d MMMM yyyy}"
            Me.DailyCalendar.DayLineColor = System.Drawing.Color.FromArgb(CType(215, Byte), CType(177, Byte), CType(0, Byte))
            Me.DailyCalendar.Dock = System.Windows.Forms.DockStyle.Fill
            Me.DailyCalendar.EnableEditInPlace = False
            Me.DailyCalendar.ExtraBreakHourColor = System.Drawing.Color.Fuchsia
            Me.DailyCalendar.FirstHour = System.TimeSpan.Parse("05:00:00")
            Me.DailyCalendar.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
            Me.DailyCalendar.FreeDay = ComponentGo.Calendars.FreeDays.None
            Me.DailyCalendar.FreeHourColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(238, Byte), CType(158, Byte))
            Me.DailyCalendar.Location = New System.Drawing.Point(0, 0)
            Me.DailyCalendar.Name = "DailyCalendar"
            Me.DailyCalendar.Size = New System.Drawing.Size(360, 448)
            Me.DailyCalendar.TabIndex = 0
            '
            'DayCalendarArea
            '
            Me.Controls.Add(Me.DailyCalendar)
            Me.Name = "DayCalendarArea"
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Properties "
        'Public Overrides ReadOnly Property Calendar() As BaseCalendar
        '    Get
        '        Return DailyCalendar
        '    End Get
        'End Property
#End Region

#Region " Time Navigation "
        Public Overrides Function SetNext() As Date
            Return SetDate(DailyCalendar.FirstDate.AddDays(1))
        End Function

        Public Overrides Function SetPrev() As Date
            Return SetDate(DailyCalendar.FirstDate.AddDays(-1))
        End Function

        Public Overrides Function SetToday() As Date
            Return SetDate(Date.Today)
        End Function

        Public Overrides Function SetDate(ByVal d As Date) As Date
            If d.Date = d Then d = d.AddHours(8)
            DailyCalendar.FirstDate = d.Date
            DailyCalendar.FirstHour = TimeSpan.FromHours(d.Hour)
            Return DailyCalendar.FirstDate
        End Function

#End Region

#Region " ToolTip "
        Protected Overrides Function GetCellToolTip(ByVal p As Point) As String
            Dim dt As DateTime = Calendar.HitDateTimeTest(p)
            If dt <> DateTime.MinValue Then
                Return dt.ToString("t")
            Else
                Return Nothing
            End If
        End Function

#End Region

        Protected Overrides Sub UpdateConfiguration()
            MyBase.UpdateConfiguration()

            DailyCalendar.VisibleColumns = AppConfig.DayViewColumnNumber
            DailyCalendar.HoursResolution = AppConfig.DayViewResolution
        End Sub


        Public Overrides Property FirstDate() As Date
            Get
                Return DailyCalendar.FirstDate
            End Get
            Set(ByVal Value As Date)
                DailyCalendar.FirstDate = Value
            End Set
        End Property

        Public Overrides ReadOnly Property HasSchedule() As Boolean
            Get
                Return True
            End Get
        End Property


        Private Sub DailyCalendar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DailyCalendar.MouseDown
            currPoint = New Point(e.X, e.Y)

            If e.Clicks < 2 Then Return
            If e.Button <> MouseButtons.Left Then Return

            If DailyCalendar.HitTest(currPoint) = DailyCalendar.Regions.ResourcesLabel Then
                Dim ri As Integer = DailyCalendar.HitResourceTest(currPoint)
                If (ri >= 0) And (ri < DailyCalendar.Resources.Count) Then
                    Dim r As Resource = DailyCalendar.Resources(ri)
                    If Not r Is Nothing Then
                        EmployeeForm.OpenForm(r.Tag)
                    End If
                End If
            ElseIf DailyCalendar.HitAppointmentTest(currPoint) Is Nothing Then
                Dim app As DataSetAppointments.Appointment = Helpers.AppointmentHelper.NewAppointment(DailyCalendar.HitDateTimeTest(currPoint))
                Dim ind As Long = DailyCalendar.HitResourceTest(currPoint)
                If ind < 0 Or ind >= DailyCalendar.Resources.Count Then Exit Sub
                Dim res As Resource = DailyCalendar.Resources(ind)
                Dim empl As DataSetData.Employee = res.Tag
                app.PerformerID = empl.EmployeeID
                AppointmentForm.OpenForm(app)
            End If

            If DailyCalendar.HitTest(New Point(e.X, e.Y)) = ResourceScheduler.Regions.ResourcesLabel Then
                EmployeeForm.OpenForm(DailyCalendar.Resources(DailyCalendar.HitResourceTest(New Point(e.X, e.Y))).Tag)
            End If
        End Sub

        Public Overrides Sub Paste(ByVal arr As IList)
            If arr Is Nothing Then
                Return
            End If
            For Each obj As DataRow In arr
                Dim item As DataAccess.DataSetAppointments.Appointment = Helpers.AppointmentHelper.Copy(obj)
                item.StartTime = DailyCalendar.SelectedDateBegin  'CurrentDate
                item.PerformerID = DailyCalendar.Resources(DailyCalendar.HitResourceTest(currPoint)).Tag.EmployeeID
                Dim m As Integer = item.ExpectedDuration Mod 60
                Dim h As Double = item.ExpectedDuration / 60 - m / 60
                item.FinishTime = item.StartTime.AddHours(h)
                item.FinishTime = item.FinishTime.AddMinutes(m)
                If Helpers.AppointmentHelper.IsValidTime(item.StartTime, item.FinishTime, item) Then
                    Helpers.AppointmentHelper.UpdateAppointment(item, Me)
                Else
                    AppointmentForm.OpenForm(item)
                End If
            Next
        End Sub

        Private Sub DailyCalendar_FirstDateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DailyCalendar.FirstDateChanged
            OnFirstDateChanged(New DateRangeEventArgs(DailyCalendar.FirstDate, DailyCalendar.FirstDate))
        End Sub

    End Class
End Namespace
